﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FW;
using Kcadastro.classes;

namespace Kcadastro.os
{
    public partial class osNew : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            masterPageKcad m = Master as masterPageKcad;
            m.idFerr = 4;
            if (!IsPostBack)
            {
                carregar();
            }
        }

        private void carregar()
        {
            //oss o = new oss();
            //cbl_servicos.DataSource = o.listaServicos();
            //cbl_servicos.DataTextField = "nome";
            //cbl_servicos.DataValueField = "PK_IDserv";
            //cbl_servicos.DataBind();
            //o = null;

            //cbl_servicos.Items[0].Selected = true;
            //cbl_servicos.Items[0].Enabled = false;

        }

        protected void bt_gravar_Click(object sender, EventArgs e)
        {
            oss o = new oss();
            

            tb_o to = new tb_o();
            to.nomeOS = tb_nome.Text;
            to.FK_IDuser = Convert.ToInt16(Session["IDuser"].ToString());
            to.arquivoOK = false;
            to.caminhoFinal = "";
            to.caminhoOriginal = "";
            to.dt_reg = DateTime.Now;
            to.FK_IDemp = Convert.ToInt16(Session["IDemp"].ToString());
            to.orcamentoOK = false;
            to.pagaOK = false;
            to.processando = false;
            to.qtd_processada = 0;
            to.qtd_registros = 0;
            to.status = "configuração inicial";
            to.stts = 1;
            to.terminoOK = false;
            to.valorOrcamento = (decimal)0;
            to.tpps = Convert.ToInt16(dd_tpps.SelectedValue);


            int idos = o.abrirOS(to);

            Session.Add("IDos", idos.ToString());
            Session.Add("msg", "OS criada com sucesso.");

            //List<int> valores = new List<int>();
            //for (int i = 0; i < this.cbl_servicos.Items.Count; i++)
            //{
            //    if (cbl_servicos.Items[i].Selected)
            //    {
            //        valores.Add(Convert.ToInt16(cbl_servicos.Items[i].Value));
            //    }
            //}

            //o.gravarServicos(idos, valores.ToArray());
            if (fu_original.HasFile)
            {
                fu_original.SaveAs(ConfigurationManager.AppSettings["arqPath"].ToString() + to.FK_IDemp.ToString() + "/" + idos.ToString() + "/original.txt");

                o.updateArqOriginal(idos, ConfigurationManager.AppSettings["arqPath"].ToString() + to.FK_IDemp.ToString() + "/" + idos.ToString() + "/original.txt");
                dbiparser.importCSV imp = new dbiparser.importCSV();
                //imp.validarArquivo(ConfigurationManager.AppSettings["arqPath"].ToString() + to.FK_IDuser.ToString() + "/" + idos.ToString() + "/original.txt");
            }
            
            o = null;

            Response.Redirect("~/os/osView.aspx");

        }
    }
}